<?php
//添加团购页面
include("../common.inc.php");
InitGP(array("page","action","aid","delids")); //初始化变量全局返回
include("function_common.php");

@set_time_limit(0); //设置脚本运行超时时间不限制


if(empty($action)){
	$title=get_cookie('emailtitle');
	$body=get_cookie('emailbody');
	
	include("tpl/email_send.htm");
}elseif ($action=="send") {
	InitGP(array("page","title","body","cityid")); //初始化变量全局返回
	if (!empty($title) && !empty($body)) {
		set_cookie('emailtitle',$title);
		set_cookie('emailbody',$body);
		include_once(INC_PATH."/sendmail.class.php");
		$sendmail=new SendEmail();
		include_once(INC_PATH."/mail.class.php");
		$mail=new MailClass();
		if(GetNum($cityid)){
			$wherestr[]="cityid = '{$cityid}'";
		}
		$wherestr[]="state = 1";
		if(!empty($wherestr)) $wheresql = implode(' AND ', $wherestr);	//条件汇总
		//获取当前页码
		$total=$mail->getcount($wheresql); 							  //总信息数
		$pagesize=10;												  //一页发送邮件数量
		$page = isset($page) ? max(1, intval($page)) : 1;             //处理页码变量
		$offset=($page-1)*$pagesize;							  //偏移量
		$dataarray=$mail->getdata("$offset,$pagesize",$wheresql); //获取团购数据
		$sendmail->run($title,$body,$dataarray);
		//print($sendmail->printmsg); //输出发送结果
		//执行跳转 跳转到第二页 保存发送内容到cookie
		if( ($page*$pagesize) < $total){
			$gourl=PHP_SELF."?action=send&page=".($page+1);
			showmsg($sendmail->printmsg,$gourl,0,100);
		}else{
			echo "<br />";
			echo $sendmail->printmsg."本次發送結束！";
		}

//		showmsg($sendmail->printmsg."本次發送結束！","email_send.php",0,1000);
		
	}elseif (get_cookie('emailtitle')){
		$title=get_cookie('emailtitle');
		$body=get_cookie('emailbody');
		include_once(INC_PATH."/sendmail.class.php");
		$sendmail=new SendEmail();
		include_once(INC_PATH."/mail.class.php");
		$mail=new MailClass();
		if(GetNum($cityid)){
			$wherestr[]="cityid = '{$cityid}'";
		}
		$wherestr[]="state = 1";
		if(!empty($wherestr)) $wheresql = implode(' AND ', $wherestr);	//条件汇总
		//获取当前页码
		$total=$mail->getcount($wheresql); 							  //总信息数
		$pagesize=10;												  ///一页发送邮件数量
		$page = isset($page) ? max(1, intval($page)) : 1;             //处理页码变量
		$offset=($page-1)*$pagesize;							  //偏移量
		$dataarray=$mail->getdata("$offset,$pagesize",$wheresql); //获取团购数据
		$sendmail->run($title,$body,$dataarray);
//		var_dump($dataarray);
//		echo"<hr>";
		//print($sendmail->printmsg); //输出发送结果
		//执行跳转 跳转到第二页 保存发送内容到cookie		
		if(($page*$pagesize)<$total){
			$gourl=PHP_SELF."?action=send&page=".($page+1);
			showmsg($sendmail->printmsg,$gourl,0,100);
		}else{
			echo "<br />";
			echo $sendmail->printmsg."本次發送結束！";
		}
//		 showmsg("本次發送結束！","email_send.php",0,100);
		
	}

}
?>